using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Core;
using Core.Graphics;
using Core.Mapping;
using SerializationUtilities;
using Microsoft.Xna.Framework.Graphics;

namespace WorldEditor.Tiles
{
    public partial class TileSetManagerUser : UserControl
    {
        TileSet CurrentTileSet;
        GraphicsDevice _Device;

        public TileSetManagerUser(GraphicsDevice GraphicsDevice)
        {
            InitializeComponent();

            _Device = GraphicsDevice;

            LoadListBox();
        }

        private void LoadListBox()
        {
            //Clear old items, if there are any
            lbTileSets.Items.Clear();
            //for each TileSet load it as an item
            foreach (KeyValuePair<int, TileSet> ts in TileSetCollection.GlobalTerrainTileSets)
            {
                lbTileSets.Items.Add(ts.Value);
            }
            //if there were any TileSets then set the selected item
            if (lbTileSets.Items.Count > 0)
            {
                lbTileSets.SelectedItem = lbTileSets.Items[0];
            }
        }
        //TODO: Remove does not actually delete the texture from the save, just removes it from the TileSet collection. True remove
        // should delete the file and update the xml file.
        protected void Remove()
        {
            TileSetCollection.GlobalTerrainTileSets.Remove(((TileSet)lbTileSets.SelectedItem).ID);
            LoadListBox();
        }

        protected void Export()
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                TileSet ts = (TileSet)(lbTileSets.SelectedItem);

                SerializationUtilities.Utilities<TileSet> Utilities = new SerializationUtilities.Utilities<TileSet>();

                Utilities.WriteObjectToFile(ts, saveFileDialog1.OpenFile(), SerializationType.XML, CompressionType.SevenZip);
            }
        }

        protected void Add()
        {
            AddTileSet ats = new AddTileSet(_Device, TileWorld.CurrentWorld.diTextures, TileSetTypes.Terrain);

            ats.ShowDialog(this);

            //After add is complete reload the list box
            LoadListBox();
        }

        protected void Import()
        {
            ImportTileSet its = new ImportTileSet(_Device, TileSetTypes.Terrain);

            its.ShowDialog(this);

            //Reload TileSets after importing
            LoadListBox();
        }

        private void LoadTiles()
        {
            //Get Current TileSet Name
            CurrentTileSet = (TileSet)lbTileSets.SelectedItem;
            

            txtTileSetName.Text = CurrentTileSet.Name;
            this.Text = "Edit Tile Set: " + CurrentTileSet.Name;

            dgvTiles.Rows.Clear();

            foreach (TileSetTile t in CurrentTileSet.Tiles)
            {
                int id = dgvTiles.Rows.Count;
                dgvTiles.Rows.Add();
                dgvTiles.Rows[id].Height = 32;
                dgvTiles.Rows[id].Cells[0].Value = TileSetTileBitmaps.GetTileBitmap(t.SpriteID, TileSetTypes.Terrain);
                dgvTiles.Rows[id].Cells[1].Value = t.SpriteID.ID.ToString();
            }
            //Set the current selected Item to the 0'th index.  This will also active
            // the index changed and make the property panel point at the 0'th item
            dgvTiles.Rows[0].Selected = true;
        }

        private void lbTileSets_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadTiles();
        }

        private void dgvTiles_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvTiles.SelectedRows.Count > 0)
            {
                if (dgvTiles.SelectedRows[0].Cells[1].Value != null)
                {
                    TileSetTile t = CurrentTileSet.Tiles[Convert.ToInt32(dgvTiles.SelectedRows[0].Cells[1].Value.ToString())];

                    if (t != null)
                    {
                        propertyGrid1.SelectedObject = t;
                    }
                }
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            Remove();
        }

        private void tsbExport_Click(object sender, EventArgs e)
        {
            Export();
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void tsbImport_Click(object sender, EventArgs e)
        {
            Import();
        }

    }
}
